package com.jinzhi.jzweb.controller;


import java.util.*;

import com.jinzhi.jzweb.domain.JazSysProjectDO;
import com.jinzhi.jzweb.domain.ZykManagementSercetKeyDO;
import com.jinzhi.jzweb.domain.ZykResourcesChaptersDO;
import com.jinzhi.jzweb.service.JazSysProjectService;
import com.jinzhi.jzweb.service.ZykManagementSercetKeyService;
import com.jinzhi.jzweb.service.ZykResourcesChaptersService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.jzweb.domain.ZykManagementResourcesDO;
import com.jinzhi.jzweb.service.ZykManagementResourcesService;
import com.jinzhi.common.utils.Result;

/**
 * 
 * <pre>
 * 资源管理表
 * </pre>
 * <small> 2023-12-07 10:56:17 | lance</small>
 */
@Controller
@RequestMapping("/jzweb/zykManagementResources")
public class ZykManagementResourcesController extends BaseController {
	@Autowired
	private ZykManagementResourcesService zykManagementResourcesService;

	@Autowired
	private ZykResourcesChaptersService zykResourcesChaptersService; //资源章节表

	@Autowired
	private JazSysProjectService jazSysProjectService;  //类型表

	@Autowired
	private ZykManagementSercetKeyService zykManagementSercetKeyService;  //类型表


	@GetMapping()
	@RequiresPermissions("jzweb:zykManagementResources:zykManagementResources")
	String ZykManagementResources(){
	    return "jzweb/zykManagementResources/zykManagementResources";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("jzweb:zykManagementResources:zykManagementResources")
	public Result<Page<ZykManagementResourcesDO>> list(ZykManagementResourcesDO zykManagementResourcesDTO){

		EntityWrapper<ZykManagementResourcesDO> wrapper = new EntityWrapper();
		if (zykManagementResourcesDTO.getName()!=null&&!zykManagementResourcesDTO.getName().equals("")) {
			wrapper.like("name", "%" + zykManagementResourcesDTO.getName() + "%");
		}
		if (zykManagementResourcesDTO.getProjectId()!=null&&!zykManagementResourcesDTO.getProjectId().equals("")) {
			wrapper.eq("project_id", zykManagementResourcesDTO.getProjectId());
		}
//		wrapper.orderBy("sort", false);
		wrapper.orderBy("creater_date", false);

		// 查询列表数据
		Page<ZykManagementResourcesDO> page = zykManagementResourcesService.selectPage(getPage(ZykManagementResourcesDO.class), wrapper);
		List list = new ArrayList();
		for (int i = 0; i < page.getRecords().size(); i++) {
			ZykManagementResourcesDO obj = (ZykManagementResourcesDO) page.getRecords().get(i);
			EntityWrapper<ZykResourcesChaptersDO> zykResourcesChaptersWrapper = new EntityWrapper<>();
			zykResourcesChaptersWrapper.eq("management_resources_id",obj.getId());
			zykResourcesChaptersWrapper.eq("level",1);
			int z = zykResourcesChaptersService.selectCount(zykResourcesChaptersWrapper); //章数量
			zykResourcesChaptersWrapper = new EntityWrapper<>();
			zykResourcesChaptersWrapper.eq("management_resources_id",obj.getId());
			zykResourcesChaptersWrapper.eq("level",2);
			int j = zykResourcesChaptersService.selectCount(zykResourcesChaptersWrapper); //节数量
			obj.setZCount(z);
			obj.setJCount(j);
			JazSysProjectDO jazSysProjectDO = jazSysProjectService.selectById(obj.getProjectId());
			obj.setProjectId(jazSysProjectDO.getName());
			list.add(obj);
		}
		page.setRecords(list);
        return Result.ok(page);
	}
	
	@GetMapping("/add")
	@RequiresPermissions("jzweb:zykManagementResources:add")
	String add(){
	    return "jzweb/zykManagementResources/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("jzweb:zykManagementResources:edit")
	String edit(@PathVariable("id") Long id,Model model){
		ZykManagementResourcesDO zykManagementResources = zykManagementResourcesService.selectById(id);
		model.addAttribute("zykManagementResources", zykManagementResources);
	    return "jzweb/zykManagementResources/edit";
	}
	
	@Log("添加资源管理表")
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("jzweb:zykManagementResources:add")
	public Result<String> save( ZykManagementResourcesDO zykManagementResources){
		zykManagementResources.setCreaterName(this.getUsername());
		zykManagementResources.setCreaterDate(new Date());
		zykManagementResourcesService.insert(zykManagementResources);
        return Result.ok();
	}
	
	@Log("修改资源管理表")
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("jzweb:zykManagementResources:edit")
	public Result<String> update( ZykManagementResourcesDO zykManagementResources){
		zykManagementResources.setUpdateDate(new Date());
		zykManagementResources.setUpdateName(this.getUsername());
		boolean update = zykManagementResourcesService.updateById(zykManagementResources);
		return update ? Result.ok() : Result.fail();
	}
	
	@Log("删除资源管理表")
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("jzweb:zykManagementResources:remove")
	public Result<String>  remove( Long id){
		Wrapper<ZykManagementSercetKeyDO> wrapper =new EntityWrapper<>();
		wrapper.eq("management_id",id);
		int count = zykManagementSercetKeyService.selectCount(wrapper);
		if (count>0){
			return Result.fail("删除失败,该资源已绑定秘钥");
		}
		zykManagementResourcesService.deleteById(id);
        return Result.ok();
	}
	
	@Log("批量删除资源管理表")
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("jzweb:zykManagementResources:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Long[] ids){
		zykManagementResourcesService.deleteBatchIds(Arrays.asList(ids));
		return Result.ok();
	}
	
}
